/**
 * @filename:UserController 2019年4月9日
 * @project user_center  V1.0
 * Copyright(c) 2018 BianPeng Co. Ltd.
 * All right reserved.
 */
package com.association.controller;

import java.util.List;

import com.association.constant.CodeEnum;
import com.association.model.ResultResp;
import com.association.model.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.association.logging.aop.log.WebLog;

import com.github.pagehelper.PageInfo;
import com.association.model.entity.User;
import com.association.service.UserService;
import com.len.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @Description:  用户接口层
 * @Author:       BianPeng
 * @CreateDate:   2019年4月9日
 * @Version:      V1.0
 *
 */
@Api(description = "用户",value="用户" )
@RestController
@RequestMapping("/user")
public class UserController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	public UserService userService;


	/**
	 * @explain 查询用户对象  <swagger GET请求>
	 * @param   对象参数：id
	 * @return  user
	 * @author  BianPeng
	 * @time    2019年4月9日
	 */
	@GetMapping("/getUserById/{id}")
	@WebLog("查询用户对象")
			@ApiOperation(value = "获取用户信息", notes = "获取用户信息[user]，作者：BianPeng")
	@ApiImplicitParam(paramType="path", name = "id", value = "用户id", required = true, dataType = "Long")
	public JsonResult getUserById(@PathVariable("id")Long id){
		JsonResult result=new JsonResult();
		try {
			User user=userService.selectByPrimaryKey(id);
			if (user!=null) {
				result.setCode(1);
				result.setMsg("成功");
				result.setData(user);
			} else {
				logger.error("获取用户失败ID："+id);
				result.setCode(-1);
				result.setMsg("你获取的用户不存在");
			}
		} catch (Exception e) {
			logger.error("获取用户执行异常："+e.getMessage());
			result.setCode(-1);
			result.setMsg("执行异常，请稍后重试");
		}
		return result;
	}

	/**
	 * @explain 添加用户对象
	 * @param   对象参数：user
	 * @return  int
	 * @author  BianPeng
	 * @time    2019年4月9日
	 */
	@WebLog("添加用户对象")
	@ApiOperation(value = "添加用户", notes = "添加用户[user],作者：BianPeng")
	@RequestMapping(value="/insertSelective", method= RequestMethod.POST, consumes= MediaType.APPLICATION_JSON_VALUE)
	public JsonResult insertSelective(User user){
		JsonResult result=new JsonResult();
		try {
			int rg=userService.insertSelective(user);
			if (rg>0) {
				result.setCode(1);
				result.setMsg("成功");
				result.setData(user);
			} else {
				logger.error("添加用户执行失败："+user.toString());
				result.setCode(-1);
				result.setMsg("执行失败，请稍后重试");
			}
		} catch (Exception e) {
			logger.error("添加用户执行异常："+e.getMessage());
			result.setCode(-1);
			result.setMsg("执行异常，请稍后重试");
		}
		return result;
	}

	/**
	 * @explain 删除用户对象
	 * @param   对象参数：id
	 * @return  int
	 * @author  BianPeng
	 * @time    2019年4月9日
	 */
	@PostMapping("/deleteByPrimaryKey")
	@WebLog("删除用户对象")
	@ApiOperation(value = "删除用户", notes = "删除用户,作者：BianPeng")
	@ApiImplicitParam(paramType="query", name = "id", value = "用户id", required = true, dataType = "Long")
	public JsonResult deleteByPrimaryKey(Long id){
		JsonResult result=new JsonResult();
		try {
			int reg=userService.deleteByPrimaryKey(id);
			if (reg>0) {
				result.setCode(1);
				result.setMsg("成功");
				result.setData(id);
			} else {
				logger.error("删除用户失败ID："+id);
				result.setCode(-1);
				result.setMsg("执行错误，请稍后重试");
			}
		} catch (Exception e) {
			logger.error("删除用户执行异常："+e.getMessage());
			result.setCode(-1);
			result.setMsg("执行异常，请稍后重试");
		}
		return result;
	}

	/**
	 * @explain 修改用户对象
	 * @param   对象参数：user
	 * @return  user
	 * @author  BianPeng
	 * @time    2019年4月9日
	 */
	@ApiOperation(value = "修改用户", notes = "修改用户[user],作者：BianPeng")
	@WebLog("修改用户对象")
	@PostMapping("/updateByPrimaryKeySelective")
	public ResultResp updateByPrimaryKeySelective(User user){
		try {
			int reg = userService.updateByPrimaryKeySelective(user);
			if (reg>0) {
				return new ResultResp(reg);
			} else {
				logger.error("修改用户失败ID："+user.toString());
				return new ResultResp(CodeEnum.SERVER);
			}
		} catch (Exception e) {
			logger.error("修改用户执行异常："+e.getMessage());
			return new ResultResp(CodeEnum.SERVER);
		}
	}

	/**
	 * @explain 获取匹配用户
	 * @return  List<User>
	 * @author  BianPeng
	 * @time    2019年4月9日
	 */
	@ApiOperation(value = "条件查询用户", notes = "条件查询[user],作者：BianPeng")
	@RequestMapping("/queryUserList")
	public ResultResp queryUserList(@RequestParam(required = false) String pageNum,
									@RequestParam(required = false) String pageSize,
									@RequestParam(required = false) String nickName,
									@RequestParam(required = false) String phone,
									@RequestParam(required = false) Long deptId,
									@RequestParam(required = false) String sex,
									@RequestParam(required = false) String email){
		try {
			User user = new User();
			user.setNickName(nickName);
			user.setPhone(phone);
			user.setSex(sex);
			user.setRoleId(deptId);
			user.setEmail(email);

			List<User> list = userService.queryUserList(user);
			return new ResultResp(list);
		} catch (Exception e) {
			logger.error(String.format("获取用户执行异常：%s,地址：%s", e.getMessage(), "/queryUserList"));
			return new ResultResp(CodeEnum.SERVER);
		}
	}

}